﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Field
    {
        public bool CreateOneField(Field_Info objField_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Field_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FieldID", objField_Info.FieldID));
            command.Parameters.Add(new SqlParameter(@"FieldName", objField_Info.FieldName));
            command.Parameters.Add(new SqlParameter(@"Location", objField_Info.Location));
            command.Parameters.Add(new SqlParameter(@"FarmID", objField_Info.FarmID));
            command.Parameters.Add(new SqlParameter(@"FieldTypeID", objField_Info.FieldTypeID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneField(Field_Info objField_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Field_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FieldID", objField_Info.FieldID));
            command.Parameters.Add(new SqlParameter("@FieldName", objField_Info.FieldName));
            command.Parameters.Add(new SqlParameter("@Location", objField_Info.Location));
            command.Parameters.Add(new SqlParameter("@FarmID", objField_Info.FarmID));
            command.Parameters.Add(new SqlParameter("@FieldTypeID", objField_Info.FieldTypeID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneField(int FieldID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Field_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FieldID", FieldID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllField()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Field_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public DataTable GetAllFieldByFieldType(int fieldTypeID)
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Field_SelectByFieldTypeID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FieldTypeID", fieldTypeID));
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Field_Info GetOneField(int FieldID)
        {
            Field_Info objField_Info = new Field_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Field_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"FieldID", FieldID));
            try
            {
                objConnection.Open();
                SqlDataReader ordField = command.ExecuteReader(); //OjectRecordDatareader
                ordField.Read();
                if (ordField.HasRows)
                {
                    if (ordField["FieldID"] != DBNull.Value)
                    {
                        objField_Info.FieldID = Convert.ToInt16(ordField["FieldID"]);
                    }
                    if (ordField["FieldName"] != DBNull.Value)
                    {
                        objField_Info.FieldName = Convert.ToString(ordField["FieldName"]);
                    }
                    if (ordField["Location"] != DBNull.Value)
                    {
                        objField_Info.Location = Convert.ToString(ordField["Location"]);
                    }
                    if (ordField["FarmID"] != DBNull.Value)
                    {
                        objField_Info.FarmID = Convert.ToInt16(ordField["FarmID"]);
                    }
                    if (ordField["FieldTypeID"] != DBNull.Value)
                    {
                        objField_Info.FieldTypeID = Convert.ToInt16(ordField["FieldTypeID"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objField_Info;
        }
    }
}
